package cn.ac.siom.query.action;

import cn.ac.siom.query.service.IItem_infoService;
import cn.ac.siom.query.service.IaccessService;
import cn.ac.siom.query.util.action.DefaultAction;
import cn.ac.siom.query.vo.Accessinfo;
import cn.ac.siom.query.vo.Item_info;
import cn.ac.siom.query.vo.Tab_Thesis;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@SuppressWarnings("unchecked")
@Controller
@RequestMapping(value = "/home/*")
public class Item_infoAction extends DefaultAction {
    @Resource
    private IItem_infoService item_infoService;
    @Resource
    private IaccessService iaccessService;

    /**
     * 激光通讯没用此接口
     */
    @RequestMapping(value = "item_info_list")
    public void list(HttpServletResponse response, HttpServletRequest request) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        try {
            insertAccessinfo(request,"");
            super.printObjectToList(response, "allItem_info", this.item_infoService.list());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @ResponseBody
    @RequestMapping(value="findNO_Item_info", method= RequestMethod.GET,produces="application/json;charset=UTF-8")
    public Map<String,Object> findNO(Integer i_id, HttpServletRequest request, Accessinfo accessinfo) throws SQLException {
       Map map= new HashMap<>();
        List<Item_info>  item_infos = this.item_infoService.list_NO(i_id);
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }

        String url =request.getHeader("Referer");
        if(url==null){
            accessinfo.setUrl("/");
        }else{
            accessinfo.setUrl(url);
        }
        accessinfo.setIp(ip);
        accessinfo.setName("专题会议详情");
        this.iaccessService.insertOne(accessinfo);
        Integer count=this.item_infoService.listThesis_count(i_id);
        map.put("item_infos",item_infos);
        map.put("count",count);
        return map;
    }

    @ResponseBody
    @RequestMapping(value="findPage_Item_info_tab_Thesis", method= RequestMethod.GET,produces="application/json;charset=UTF-8")
    public Map<String,Object> findfindPage_Item_info_tab_Thesis(HttpServletRequest request, Integer page_id,Integer i_id) throws SQLException {
        insertAccessinfo(request,"首页会议论文");
        Map map= new HashMap<>();
        List<Tab_Thesis> tab_Thesis=this.item_infoService.listThesis_page(page_id,i_id);
         map.put("tab_Thesis",tab_Thesis);
        return map;
    }

    @ResponseBody
    @RequestMapping(value="findPage_Item_info", method= RequestMethod.GET,produces="application/json;charset=UTF-8")
    public Map<String, Object> findPage(Integer rownum,Integer page_id,HttpServletRequest request,Accessinfo accessinfo) throws SQLException {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        String url =request.getHeader("Referer");
        if(url==null){
            accessinfo.setUrl("/");
        }else{
            accessinfo.setUrl(url);
        }
        accessinfo.setIp(ip);
        accessinfo.setName("首页专题会议");
        this.iaccessService.insertOne(accessinfo);
        List<Item_info>  item_infos = this.item_infoService.list_page(rownum,page_id);
        Integer cont=this.item_infoService.list_count();
        Map<String,Object> map = new HashMap<>();
        map.put("item_infos",item_infos);
        map.put("cont",cont);
        map.put("page_id",page_id);
        return map;
    }

    @ResponseBody
    @RequestMapping(value="findCount_Item_info", method= RequestMethod.GET,produces="application/json;charset=UTF-8")
    public int findCount() throws SQLException {
        return this.item_infoService.list_count();
    }

    @ResponseBody
    @RequestMapping(value="findhttp", method= RequestMethod.GET,produces="application/json;charset=UTF-8")
    public int findhttp() throws SQLException {
        return this.item_infoService.list_count();
    }

    @ResponseBody
    @RequestMapping(value="findwords", method= RequestMethod.GET,produces="application/json;charset=UTF-8")
    public List<Item_info> findwords(String words) throws SQLException {
        return this.item_infoService.list_words(words);
    }

    @Override
    public String getText() {
        return null;
    }

}